﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IForm_EditShortcut</summary>
    public class IForm_EditShortcut : IForm
    {
        public int Value; // Klavesova skratka pre editaciu
        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_None, FBtn_OK, FBtn_Cancel;
        protected ICheckBox FCheck_Ctrl, FCheck_Alt, FCheck_Shift;
        protected IComboBox FCombo_Keys;

        /// <summary>Konstruktor</summary>
        public IForm_EditShortcut()
        {
            Value = ICmd.KEY_NONE;
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 376, 140); System.Drawing.Size CS = ClientSize;

            // Vytvorim groupbox
            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);

            // Vytvorim tlacidla
            FBtn_None = new IButton(lang.FORM_EDIT_SHORTCUT_TEXT_1, Event_BtnNoneClick); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FBtn_None, FGrp, 0, 10);
            FBtn_Cancel = new IButton(lang.FORM_EDIT_SHORTCUT_TEXT_3); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_SHORTCUT_TEXT_2, Event_BtnOKClick); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);

            FCheck_Ctrl = new ICheckBox(lang.FORM_EDIT_SHORTCUT_TEXT_4); 
            FCheck_Alt = new ICheckBox(lang.FORM_EDIT_SHORTCUT_TEXT_5);
            FCheck_Shift = new ICheckBox(lang.FORM_EDIT_SHORTCUT_TEXT_6);

            // Combo box s klavesami
            FCombo_Keys = new IComboBox(); FCombo_Keys.Width = 140; FCombo_Keys.AddItem(lang.FORM_EDIT_SHORTCUT_TEXT_7, ICmd.KEY_NONE);
            for (int i = 1; i < ICmd.KEY_LAST; i++) FCombo_Keys.AddItem(ICmd.Key2Str(i), i);

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FCheck_Ctrl, FCheck_Alt, FCheck_Shift, FCombo_Keys });
            IControl.PlaceControl_Inside_LeftTop_LeftTop(FCheck_Ctrl, FGrp, 20, 20); // Pozicia az po pridani do controls
            IControl.PlaceControl_Outside_LeftTop_TopRight(FCheck_Alt, FCheck_Ctrl, 0, 10);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FCheck_Shift, FCheck_Alt, 0, 10);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FCombo_Keys, FCheck_Shift, -2, 20);

            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel, FBtn_None }); // Zaradim do controls

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus();

            FillForm(Value); // Vyplnim
            return true; // Vsetko OK
        }
        /// <summary>Vyplnenie formulara</summary>
        /// <param name="pValue">klav. skratka</param>
        protected void FillForm(int pValue)
        {
            FCombo_Keys.SelectedIntID = pValue & 0xff; // Nastavim klavesu
            if ((pValue & 0xff) == ICmd.KEY_NONE) pValue = ICmd.KEY_NONE;
            FCheck_Ctrl.Checked = (pValue & ICmd.KEY_CTRL) != 0; // Nastavim priznaky kontrolnych klaves
            FCheck_Alt.Checked = (pValue & ICmd.KEY_ALT) != 0;
            FCheck_Shift.Checked = (pValue & ICmd.KEY_SHIFT) != 0;
        }
        /// <summary>Nacitanie obsahu formulara do klav. skratky</summary>
        /// <returns>klav. skratka</returns>
        protected int ReadForm()
        {
            int R = FCombo_Keys.SelectedIntID;
            if (R == ICmd.KEY_NONE) return ICmd.KEY_NONE; // Ziadna klavesa?
            if (FCheck_Ctrl.Checked) R |= ICmd.KEY_CTRL; // Pridam kontrolne klavesy
            if (FCheck_Alt.Checked) R |= ICmd.KEY_ALT;
            if (FCheck_Shift.Checked) R |= ICmd.KEY_SHIFT;
            return R;
        }

        /// <summary>Event: OnClick pre tlacidlo FBtn_None</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnNoneClick(object pSender, EventArgs e)
        {
            FillForm(ICmd.KEY_NONE); // Zrusim klavesovu skratku
        }
        /// <summary>Event: OnClick pre tlacidlo FBtn_OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnOKClick(object pSender, EventArgs e)
        {
            Value = ReadForm(); // Nacitam vyslednu skratku
            DialogResult = System.Windows.Forms.DialogResult.OK; // Koncim
        }
    }
}
